package com.toune.movie.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.toune.movie.R;
import com.toune.movie.base.Api;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.mvp.adapter.AdapterHistory;
import com.toune.movie.mvp.contract.HistoryContract;
import com.toune.movie.mvp.presenter.HistoryPresenter;
import com.vondear.rxtool.RxDeviceTool;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryActivity extends BaseActivity<HistoryPresenter> implements HistoryContract.View {
    @BindView(R.id.rclview)
    RecyclerView rclview;
    private AdapterHistory historyAdapter;

    @Override // 实例化presenter
    public HistoryPresenter initPresenter() {
        return new HistoryPresenter();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    @Override
    public void init(Bundle savedInstanceState) {
        MyUtil.showProDialog(context);
        AVQuery<AVObject> avObjectAVQuery = new AVQuery<>(AppConstant.TABLE_HIS);
        avObjectAVQuery.orderByDescending("updatedAt");
        if (AVUser.getCurrentUser() != null) {
            avObjectAVQuery.whereMatches("userId", AVUser.getCurrentUser().getObjectId());
        } else {
            avObjectAVQuery.whereMatches("phoneId", RxDeviceTool.getUniqueSerialNumber());
        }
        avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    notifyAdapter(list);
                } else {
                    e.printStackTrace();
                }
                MyUtil.hidProDialog();
            }
        });
    }

    private void notifyAdapter(final List<AVObject> list) {
        if (historyAdapter == null) {
            historyAdapter = new AdapterHistory(R.layout.dialog_history, list);
            rclview.setLayoutManager(new LinearLayoutManager(context));
            historyAdapter.setOnItemClickListener(
                    new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            AVObject history = list.get(position);
                            int tag = history.getInt("tag");
                            Intent intent = new Intent();
                            if (tag == 0) {
                                intent.setClass(context, PlayVideoActivity.class);
                            } else {
                                intent.setClass(context, WebActivity.class);
                            }
                            intent.putExtra("title", history.getString("title"));
                            intent.putExtra("url", history.getString("url"));
                            startActivity(intent);
                        }
                    });
            rclview.setAdapter(historyAdapter);
        } else {
            historyAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_history;
    }

    @Override
    protected String getTitleStr() {
        return "历史记录";
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
